package client;

import client.Immagine;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.sound.midi.InvalidMidiDataException;
import javax.sound.midi.MidiUnavailableException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import org.w3c.dom.css.CSSPrimitiveValue;
import org.w3c.dom.css.RGBColor;

import common.ClientInterface;
import common.ComandiException;

public class Intro extends JFrame{
	
	private static final long serialVersionUID = 1L;
	private JButton creaUtente=new JButton("CREA UTENTE");
	private JButton login=new JButton("LOGIN");
	private JButton uscita=new JButton("USCITA");
	private Client client;
	
	private String user;
	private String pass;
	
	private Grafica grafica;
	
	private JButton sound;
	private JPanel panel_north;
	
	
	public Intro(Client client, Grafica grafica) {
		
		super("L'isola Dei Dinosauri");
		
		this.client=client;
		this.grafica=grafica;
		
		/*creaUtente.setIcon(new Immagine("roccia2.png"));
	
		creaUtente.setHorizontalTextPosition(JButton.CENTER);
		creaUtente.setVerticalTextPosition(JButton.CENTER);
		login.setIcon(new Immagine("roccia.png"));
		uscita.setIcon(new Immagine("roccia.png"));
		*/
		//JPanel con Grid Layout
		JPanel panel_grid=new JPanel(new GridLayout(3,1));
		panel_grid.setBorder(BorderFactory.createTitledBorder(""));
		
		panel_grid.add(creaUtente);
		panel_grid.add(login);
		panel_grid.add(uscita);

		
		
				
		//JFrame 
		//setExtendedState(Frame.MAXIMIZED_BOTH);
		/*Toolkit tk = Toolkit.getDefaultToolkit();  
		int xSize = ((int) tk.getScreenSize().getWidth());  
		int ySize = ((int) tk.getScreenSize().getHeight());  
		frame.setSize(xSize,ySize); */ 
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		setLayout(new BorderLayout());
		
		panel_north=new JPanel(); 
	
			
		sound= new JButton();
		sound.setIcon(new Immagine("soundOn.png")); 
		//sound.setBackground(Color.orange);
		sound.setPreferredSize(new Dimension(20, 20));
		sound.setBounds(0, 0, 20, 20);
		//sound.setHorizontalAlignment(JButton.RIGHT);
		panel_north.add(sound);
		//panel_north.setLayout(null);
		add( panel_north, BorderLayout.NORTH);
		
		//add(new JButton("South"), BorderLayout.SOUTH);
		
		JLabel immagineCenter= new JLabel();
		immagineCenter.setIcon(new Immagine("intro.png")); 
		immagineCenter.setHorizontalAlignment( JLabel.CENTER );
		immagineCenter.setVerticalAlignment(JLabel.CENTER );
		add(immagineCenter, BorderLayout.CENTER);
			
		add(panel_grid, BorderLayout.EAST);
		//add(new JButton("West"), BorderLayout.WEST);
		pack();
		setVisible(true);
		
				
		login.setEnabled(true);
		
		sound.addActionListener(new AscoltatoreSound());
		login.addActionListener(new AscoltatoreLogin());
		uscita.addActionListener(new AscoltatoreExit());
		creaUtente.addActionListener(new AscoltatoreCreaUtente());
		
		login.setBackground(new Color(205, 184, 145));
		uscita.setBackground(new Color(205, 184, 145));
		creaUtente.setBackground(new Color(205, 184, 145));
		
	}
	
	//gestione eventi
	
	public JButton getSound() {
		return sound;
	}

	public void setSound(JButton sound) {
		this.sound = sound;
	}

	private class AscoltatoreCreaUtente implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String action=e.getActionCommand();
			
			JPasswordField pwd = new JPasswordField(10);
			user = JOptionPane.showInputDialog("Utente","giocatore");
			JOptionPane.showConfirmDialog(null, pwd,"Password",JOptionPane.OK_CANCEL_OPTION);  
			pass=new String(pwd.getPassword());
			
			if(pass==null||pass.length()==0)
				pass="g";//solo per prove
			
			if(user!=null && user.length()>0){
					
				try{
					String s=client.getServerInt().creaUtente(user,pass); 
					client.ok();
					client.setUsername(user);
				}catch(ComandiException ex){
					client.no(ex.getMessage());
				}
				catch(IOException ex){
					client.no("errore di connessione");
				}
			   
			}else
					JOptionPane.showMessageDialog(null,"Username o Password non corretti");
		
			
		}
	}
	
	private class AscoltatoreLogin implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String action=e.getActionCommand();
				
			JPasswordField pwd = new JPasswordField(10);
			user = JOptionPane.showInputDialog("Utente","giocatore");
			int p = JOptionPane.showConfirmDialog(null, pwd,"Password",JOptionPane.OK_CANCEL_OPTION);  
			pass=new String(pwd.getPassword());
			
			if(pass==null||pass.length()==0)
				pass="g";//solo per prove
			
			
			if(user!=null && user.length()>0){
				String token;	
				try{
					if(client.isRmi()==true){
						token=client.getServerInt().login(user,pass,(ClientInterface)client); 
					}
					else
						token=client.getServerInt().login(user,pass,null); 
					client.setUsername(user);
					client.ok(token);
					client.setToken(token);
					client.setLoggato(true);
					dispose();
					synchronized (client.getVisual()){
						client.getVisual().notifyAll();
					}
				}catch(ComandiException ex){
					client.setLoggato(false);
					client.no(ex.getMessage());
				}
				catch(IOException ex){
					client.no("errore di connessione");
				}
				
					
			}else
					JOptionPane.showMessageDialog(null,"Username o Password non corretti");
			
				
		
				
		}
	}
	
	private class AscoltatoreExit implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String action=e.getActionCommand();
			client.setEsci(true);
			System.out.println("FINE VIDEOGIOCO");
			System.exit(NORMAL);
		}
	}
	
	private class AscoltatoreSound implements ActionListener{
		public void actionPerformed(ActionEvent e){
			String action=e.getActionCommand();
			if(grafica.isOn()){
				grafica.getS1().stop();
				sound.setIcon(new Immagine("soundOff.png"));
				grafica.setOn(false);
			}else{
				grafica.getS1().inizializzatore();
				sound.setIcon(new Immagine("soundOn.png"));
				grafica.setOn(true);
			}
		}
	}
	

}
